//
//  WeatherView.m
//  International_Hospital
//
//  Created by LiYiLin on 16/7/4.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "WeatherView.h"
#import "WeatherView_Model.h"
#import <UIImageView+WebCache.h>
#import "NSAddressController.h"


#define TextColor [UIColor colorWithRed:37/255.0 green:174/255.0 blue:185/255.0 alpha:1]
//#define TextColor WHITE_COLOR

@interface WeatherView ()

@property (nonatomic, weak) UIImageView *dayImageView;
@property (nonatomic, weak) UILabel     *weatherText;
@property (nonatomic, weak) UIButton    *chooseCityButton;
@property (nonatomic, weak) UILabel     *temperatureText;
@property (nonatomic, weak) UILabel     *dateText;
@property (nonatomic, weak) UIView      *dividingLineView;

@end

@implementation WeatherView

- (instancetype)init {

    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 创建控件
- (void)setupUI {
    
    // 天气图片
    UIImageView *dayImageView = [[UIImageView alloc] init];
    self.dayImageView = dayImageView;
    dayImageView.layer.cornerRadius = 10;
    dayImageView.layer.masksToBounds= YES;
    [self addSubview:dayImageView];
    
    // 天气信息
    UILabel *weatherText = [[UILabel alloc] init];
    self.weatherText = weatherText;
    weatherText.textColor = TextColor;
    weatherText.textAlignment = NSTextAlignmentCenter;
    weatherText.font = [UIFont systemFontOfSize:14];
//    weatherText.lineBreakMode = UILineBreakModeWordWrap;
    weatherText.numberOfLines = 0;
    weatherText.textColor = TextColor;
    [self addSubview:weatherText];
    
    // 城市选择
    UIButton *chooseCityButton = [[UIButton alloc] init];
    self.chooseCityButton = chooseCityButton;
    chooseCityButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:chooseCityButton];
    
    // 温度
    UILabel *temperatureText = [[UILabel alloc] init];
    self.temperatureText = temperatureText;
    temperatureText.font = [UIFont systemFontOfSize:14];
    temperatureText.textColor = TextColor;
    temperatureText.textAlignment = NSTextAlignmentCenter;
    [self addSubview:temperatureText];
    
    // 日期
    UILabel *dateText = [[UILabel alloc] init];
    self.dateText = dateText;
    dateText.textColor = TextColor;
    dateText.font = [UIFont systemFontOfSize:14];
    dateText.textAlignment = NSTextAlignmentCenter;
    [self addSubview:dateText];
    
    // 添加 button 的点击事件
    [chooseCityButton addTarget:self action:@selector(chooseCityButton:) forControlEvents:UIControlEventTouchUpInside];
    
    // 接着 设置 masonry
    [self setViewMasonry];
}

#pragma mark - 添加约束
- (void)setViewMasonry {
    
    // 中间的分割线
    UIView *dividingLineView = [[UIView alloc] init];
    self.dividingLineView = dividingLineView;
    [self addSubview:dividingLineView];
    dividingLineView.backgroundColor = [UIColor grayColor];
    [dividingLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX).offset(-0.5);
        make.top.offset(10);
        make.bottom.offset(-10);
        make.width.offset(1);
    }];
    
    // 天气 图片 约束.不同的手机型号显示不同的约束
    if (kDevice_Is_iPhone6Plus) {
        [self.dayImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.left.offset(20);
            make.width.height.offset(50);
        }];
    } else if (kDevice_Is_iPhone6) {
        [self.dayImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.left.offset(15);
            make.width.height.offset(50);
        }];
    } else {
        [self.dayImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.mas_centerY);
            make.left.offset(10);
            make.width.height.offset(50);
        }];
    }
    
    
    // 天气 信息 (例如:大雨) 约束
    [self.weatherText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dayImageView.top);
        make.left.equalTo(self.dayImageView.mas_right).offset(0);
        make.right.equalTo(self.dividingLineView.mas_left).offset(0);
    }];
    
    // 城市选择 Button
    [self.chooseCityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.dayImageView.bottom).offset(8);
        make.centerX.equalTo(self.weatherText.centerX);
    }];

    // 温度
    [self.temperatureText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_centerY).offset(-25);
        make.left.equalTo(self.mas_centerX);
        make.right.offset(0);
    }];
    
    // 日期
    [self.dateText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_centerY).offset(25);
        make.left.equalTo(self.mas_centerX);
        make.right.offset(0);
    }];

}

#pragma mark - 给控件 添加文字信息
- (void)setWeatherModel:(WeatherView_Model *)weatherModel {
    
    _weatherModel = weatherModel;
    
    // 天气图片
    [self.dayImageView sd_setImageWithURL:[NSURL URLWithString:weatherModel.dayPictureUrl]];
    
    // 天气详情
    self.weatherText.text = [NSString stringWithFormat:@"%@", weatherModel.weather];
    
    // 温度
    self.temperatureText.text = [NSString stringWithFormat:@"温度: %@", weatherModel.temperature];
    
    // 日期   原文:  --->周二 07月05日 (实时：33℃)<---  一共18 个  我只需要 前面 9个 --> 周二 07月05日  所以需要截取
    if (weatherModel.date.length > 9) {
        weatherModel.date = [weatherModel.date substringWithRange: NSMakeRange(0, 9)];
    }
    
    self.dateText.text = [NSString stringWithFormat:@"日期: %@", weatherModel.date];
    
    // 天气 城市 选择
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:weatherModel.currentCity];
    NSRange strRange = {0,[str length]};
    
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    
    // 文字及下划线 颜色
    [str addAttribute:NSForegroundColorAttributeName value:TextColor range:strRange];
    [self.chooseCityButton setAttributedTitle:str forState:UIControlStateNormal];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self layoutIfNeeded];
}

#pragma mark - 点击button的方法
- (void)chooseCityButton:(UIButton *)sender {

    // 点击 pictCityButton 通过 block 回调 跳转到 选择城市 界面
    if (self.didPictCityButton) {
        
        self.didPictCityButton();
    }
}


@end

















